package net.lwf.date.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public final class DateUtil {

	  // Default date format
	  private static final String DATEFORMAT = "MM/dd/yyyy";

	  /**
	   * Method to format a date object to a string of supplied format If the
	   * format is null, the default MM/dd/yyy is assumed Returns null if the

	   * format string is invalid.
	   *
	   * @param <b>date </b>The date object to be formatted
	   * @param <b>format </b> The date format
	   *
	   * @return <b>date</b> The string representation of date
	   */
	    public static String format(Date date, String format) {
			return format(date, format, null);
		}
	 
	  /**
	   * Method to format a date object to a string of supplied format and locale
	   * If the format is null, the default MM/dd/yyy is assumed if locale is
	   * null, the default locale of the system is assumed Returns null if the
	   * format string is invalid.
	   *
	   * @param <b>date </b>The date object to be formatted
	   * @param <b>format </b> The date format
	   * @param <b>locale </b> The locale
	   *
	   * @return <b>date</b> The string representation of date
	   */
	  public static String format(Date date, String format, Locale locale) {
	    try {
				if (format == null) {
					format = DATEFORMAT;
				}

				if (locale == null) {
					locale = Locale.getDefault();
				}
				SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
				return sdf.format(date);
			} catch (Exception ex) {
				// throw new UtilityException("Exception thrown from format " +
				// "method of Utilities class : " +
				// ex.getMessage());
			}
			return null;
		}

	  /**
	   * Method to parse a date string in the format MM/dd/yyy to a Date object
	   * This method returns null if the input string does not match the format.
	   *
	   * @param <b>dateString </b> The string to be converted to a Date
	   *
	   * @return <b>Date</b>The parsed date object
	   */
	    public static Date parse(String dateString) {
	    	return parse(dateString, null);
	    }
	  /**
	   * Method to parse a date string in the supplied format to a Date object. If
	   * the format is null, the default MM/dd/yyy is assumed and returns null if
	   * the input string does not match the format.
	   *
	   * @param <b>dateString </b> The string to be converted to a Date
	   * @param <b>formatString </b> The format of the string
	   *
	   * @return <b>Date</b> The Date object of the string parsed
	   */
	    public static Date parse(String dateString, String formatString) {
		    try {
		
		      if(formatString == null) {
		        formatString = DATEFORMAT;
		      }
		
		      SimpleDateFormat sdf = new SimpleDateFormat(formatString);
		
		
		      return sdf.parse(dateString);
		    } catch(Exception ex) {
		//      throw new UtilityException("Exception thrown from parse " +
		//                                 "method of Utilities class of given " +
		//                                 "status : " + ex.getMessage());
		    }
		    return null;
	    }
	  /**
	   * Method to format a date object to a MM/dd/yyy string.
	   *
	   * @param <b>date </b>The date object to be formatted
	   *
	   * @return <b>date</b> String representation of date
	   */
	    public static String format(Date date) {
			return format(date, null);
		}
	     

	    //---------------------------------------------------------------------------------------------
	    // Utilities based on Calendar
	    //---------------------------------------------------------------------------------------------
	    public static Calendar toCalendar(Date date) {
	    	Calendar cal = Calendar.getInstance();
	    	cal.setTime(date);
	    	return cal;
	    }
	    
	    public static Date toDate(Calendar cal) {
	    	return cal.getTime();
	    }
	    
	    public static Date resetTime(int hour, int minute, int second) {
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.HOUR_OF_DAY, hour);
			cal.set(Calendar.MINUTE, minute);
			cal.set(Calendar.SECOND, second);
			cal.set(Calendar.MILLISECOND, 0);
			return cal.getTime();
		}
	    
	    public static Date getCurrentDate() {
	    	return Calendar.getInstance().getTime();
	    }
	    
	   /** 
	    * Get the date java.util.Date object for days before current date 
	    * 
	    * @param days 
	    * @return 
	    */  
	    public static Date getDateBeforeDays(int days) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, -days); // -days
			return cal.getTime();
		}  
	/*    public static Date getDateBeforeDays(int days) {
			long backDateMS = System.currentTimeMillis() - ((long) days) * 24 * 60 * 60 * 1000;
			Date backDate = new Date();
			backDate.setTime(backDateMS);
			return backDate;
		} */
	      
	    /** 
	    * Get the date java.util.Date object for days after current date 
	    * 
	    * @param days 
	    * @return 
	    */  
	    public static Date getDateAfterDays(int days) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, days); // +days
			return cal.getTime();
		}  
	/*    public static Date getDateAfterDays(int days) {
			long backDateMS = System.currentTimeMillis() + ((long) days) * 24 * 60 * 60 * 1000;
			Date backDate = new Date();
			backDate.setTime(backDateMS);
			return backDate;
		}*/
	      
	    /** 
	    * Get the date java.util.Date object for years after current date 
	    * 
	    * @param years 
	    * @return 
	    */  
	    public static Date getDateAfterYears(int years) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.YEAR, years); // +years
			return cal.getTime();
		}  
	      
	    /** 
	    * Get the date java.util.Date object for years before current date 
	    * 
	    * @param years 
	    * @return 
	    */  
	    public static Date getDateBeforeYears(int years) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.YEAR, -years); // -years
			return cal.getTime();
		}  
	      
	    /** 
	    * Get the date java.util.Date object for months after current date 
	    * 
	    * @param months 
	    * @return 
	    */  
	    public static Date getDateAfterMonths(int months) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.MONTH, months); // +months
			return cal.getTime();
		}  
	      
	    /** 
	    * Get the date java.util.Date object for months before current date 
	    * 
	    * @param months 
	    * @return 
	    */  
	    public static Date getDateBeforeMonths(int months) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.MONTH, -months); // -months
			return cal.getTime();
		}  
	      
	    /** 
	    * Get the date java.util.Date object for hours before current date 
	    * 
	    * @param hours 
	    * @return 
	    */  
	    public static Date getDateBeforeHours(int hours) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.HOUR, -hours); // -hours
			return cal.getTime();
		}  
	      
	    /** 
	    * Get the date java.util.Date object for hours after current date 
	    * 
	    * @param hours 
	    * @return 
	    */  
	    public static Date getDateAfterHours(int hours) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.HOUR, hours); // +hours
			return cal.getTime();
		}  
	      
	    /** 
	    * Get the date java.util.Date object for minutes before current date 
	    * 
	    * @param minutes 
	    * @return 
	    */  
	    public static Date getDateBeforeMinutes(int minutes) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.MINUTE, -minutes); // -minutes
			return cal.getTime();
		}  
	      
	    /** 
	    * Get the date java.util.Date object for minutes after current date 
	    * 
	    * @param minutes 
	    * @return 
	    */  
	    public static Date getDateAfterMinutes(int minutes) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.MINUTE, minutes); // +minutes
			return cal.getTime();
		}  
	      
	    /** 
	    * Get the date java.util.Date object for seconds before current date 
	    * 
	    * @param seconds 
	    * @return 
	    */  
	    public static Date getDateBeforeSeconds(int seconds) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.SECOND, -seconds); // -seconds
			return cal.getTime();
		}  
	      
	    /** 
	    * Get the date java.util.Date object for seconds after current date 
	    * 
	    * @param seconds 
	    * @return 
	    */  
	    public static Date getDateAfterSeconds(int seconds) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.SECOND, seconds); // +seconds
			return cal.getTime();
		}
		
		
		/**
		 * @param args
		 */
		public static void main(String[] args) {
			// TODO Auto-generated method stub

		}

	}
